import { TagView } from './state'
import { Mutations } from './mutations'
import { TagsMutationTypes } from './mutation-types'
import { ActionTypes } from './action-types'


type AugmentedActionContext = {
    commit<K extends keyof Mutations>(
        key: K,
        payload?: Parameters<Mutations[K]>[1],
    ): ReturnType<Mutations[K]>
}

type NoAugmentedActionContext = {
    commit<K extends keyof Mutations>(
        key: K,
    ): ReturnType<Mutations[K]>
}

export type Actions = {
    [ActionTypes.ACTION_ADD_VIEW](
        { commit }: AugmentedActionContext,
        view: TagView
    ): void
    [ActionTypes.ACTION_ADD_VISITED_VIEW](
        { commit }: AugmentedActionContext,
        view: TagView
    ): void
    [ActionTypes.ACTION_DEL_VIEW](
        { commit }: AugmentedActionContext,
        view: TagView
    ): void
    [ActionTypes.ACTION_DEL_OTHER_VIEW](
        { commit }: AugmentedActionContext,
        view: TagView
    ): void
    [ActionTypes.ACTION_DEL_CACHED_VIEW](
        { commit }: AugmentedActionContext,
        view: TagView
    ): void
    [ActionTypes.ACTION_OTHER_VIEWS](
        { commit }: AugmentedActionContext,
        view: TagView
    ): void
    [ActionTypes.ACTION_DEL_ALL_VIEWS](
        { commit }: NoAugmentedActionContext
    ): void
    [ActionTypes.ACTION_DEL_ALL_CACHED_VIEWS](
        { commit }: NoAugmentedActionContext,
    ): void
    [ActionTypes.ACTION_UPDATE_VISITED_VIEW](
        { commit }: AugmentedActionContext,
        view: TagView
    ): void
}

export const actions: Actions = {
    async [ActionTypes.ACTION_ADD_VIEW]({ commit }, view: TagView) {
        commit(TagsMutationTypes.ADD_VISITED_VIEW, view)
        commit(TagsMutationTypes.ADD_CACHED_VIEW, view)
    },
    [ActionTypes.ACTION_ADD_VISITED_VIEW]({ commit }, view: TagView) {
        commit(TagsMutationTypes.ADD_VISITED_VIEW, view)
    },
    [ActionTypes.ACTION_DEL_VIEW]({ commit }, view: TagView) {
        commit(TagsMutationTypes.DEL_VISITED_VIEW, view)
        commit(TagsMutationTypes.DEL_CACHED_VIEW, view)
    },
    [ActionTypes.ACTION_DEL_OTHER_VIEW]({ commit }, view: TagView) {
        commit(TagsMutationTypes.DEL_OTHERS_VISITED_VIEWS, view)
        commit(TagsMutationTypes.DEL_OTHERS_CACHED_VIEWS, view)
    },
    [ActionTypes.ACTION_DEL_CACHED_VIEW]({ commit }, view: TagView) {
        commit(TagsMutationTypes.DEL_CACHED_VIEW, view)
    },
    [ActionTypes.ACTION_OTHER_VIEWS]({ commit }, view: TagView) {
        commit(TagsMutationTypes.DEL_OTHERS_VISITED_VIEWS, view)
        commit(TagsMutationTypes.DEL_OTHERS_CACHED_VIEWS, view)
    },
    [ActionTypes.ACTION_DEL_ALL_VIEWS]({ commit }) {
        commit(TagsMutationTypes.DEL_ALL_VISITED_VIEWS)
        commit(TagsMutationTypes.DEL_ALL_CACHED_VIEWS)
    },
    [ActionTypes.ACTION_DEL_ALL_CACHED_VIEWS]({ commit }) {
        commit(TagsMutationTypes.DEL_ALL_CACHED_VIEWS)
    },
    [ActionTypes.ACTION_UPDATE_VISITED_VIEW]({ commit }, view: TagView) {
        commit(TagsMutationTypes.UPDATE_VISITED_VIEW, view)
    }

}
